from flask import g, Blueprint, request, jsonify, render_template, session

from common import Result
from models import Classes, StudentClassesRel, TestPaper, Student, SignIn, TestPaperAnswer
from exts import db, get_time, to_json_all
from flask_login import login_required
from decorations import teacher_required

bp = Blueprint("teacher", __name__, url_prefix="/teacher")


# todo before_request
@bp.before_request
@login_required
@teacher_required
def before_request():
    pass


# todo /update_score
@bp.route("/update_score", methods=['POST'])
def update_score():
    score = request.values.get("score")
    id_test_paper_answer = request.values.get("id_test_paper_answer")

    test_paper_answer = db.session.query(TestPaperAnswer).get(id_test_paper_answer)

    test_paper_answer.score = score

    db.session.commit()

    r = Result()
    return r.success(msg="success")


# todo /add_class
@bp.route("/add_class", methods=['GET', 'POST'])
def add_class():
    class_name = request.values.get("class_name")
    class_room = request.values.get("class_room")
    class_time = request.values.get("class_time")

    teacher_class = Classes(
        class_name=class_name,
        class_room=class_room,
        class_time=class_time,
        student_number=0,
        create_time=get_time(),
        update_time=get_time(),
        is_delete=0,
        teacher=g.user.teacher
    )

    db.session.add(teacher_class)
    db.session.commit()

    return "success"


# todo /query_class
@bp.route("/query_class")
def query_class():
    teacher_class = g.user.teacher.classes
    data = {
        "code": 0,
        "msg": "",
        "count": len(teacher_class),
        "data": []
    }
    for i in teacher_class:
        data["data"].append({
            "id": i.id,
            "class_name": i.name,
            "student_number": len(i.student_classes_rel),
            "class_room": i.room,
            "class_time": i.time
        })

    return jsonify(data)


# todo /query_student
@bp.route("/query_student")
def query_student():
    data = {
        "code": 0,
        "msg": "",
        "count": 0,
        "data": []
    }

    all_student = []
    for a_classes in g.user.teacher.classes:
        for j in a_classes.student_classes_rel:
            all_student.append(j.student)

            data["data"].append({
                "id": j.student.id,
                "name": j.student.name,
                "class_name": a_classes.name,
                "phone": j.student.phone,
                "parent_phone": j.student.parent_phone,
                "age": j.student.age,
                "material_cost": j.student.material_cost,
                "username": j.student.user.username,
                "password": j.student.user.password
            })

    data["count"] = len(all_student)

    return jsonify(data)


# todo /query_sign_in_table
@bp.route("/query_sign_in_table")
def query_sign_in_table():
    id_classes = request.values.get("id_classes")
    data = {
        "code": 0,
        "msg": "",
        "count": 0,
        "data": []
    }

    scr = db.session.query(StudentClassesRel).filter(StudentClassesRel.id_classes == id_classes).all()
    for i in scr:
        data["data"].append({
            "id": i.id,
            "name": i.student.name,
            "phone": i.student.phone,
            "parent_phone": i.student.parent_phone,
            "sign_in_count": len(db.session.query(SignIn).filter(SignIn.id_student == i.student.id).all())
        })

    return jsonify(data)


# todo url /class_list
@bp.route("/class_list")
def class_list():
    content = {
        "user": g.user,
    }
    return render_template("teacher/class_list.html", **content)


# todo url /student_list
@bp.route("/student_list")
def student_list():
    content = {
        "user": g.user,
    }
    return render_template("teacher/student_list.html", **content)


# todo url /test_paper_list
@bp.route("/test_paper_list")
def test_paper_list():
    content = {
        "user": g.user,
    }
    return render_template("teacher/test_paper_list.html", **content)


# todo url /test_paper_list_1
@bp.route("/test_paper_list_1")
def test_paper_list_1():
    content = {
        "user": g.user,
    }
    return render_template("teacher/test_paper_list_1.html", **content)


# todo url /test_paper_list_2
@bp.route("/test_paper_list_2")
def test_paper_list_2():
    content = {
        "user": g.user,
    }
    return render_template("teacher/test_paper_list_2.html", **content)


# todo url /user
@bp.route("/user")
def user():
    content = {
        "user": g.user,
    }
    return render_template("teacher/user.html", **content)


# todo url /add_test_paper_url
@bp.route("/add_test_paper_url")
def add_test_paper_url():
    content = {
        "user": g.user,
        "classes": g.user.teacher.classes
    }
    return render_template("teacher/add_test_paper.html", **content)


# todo /check_the_exam_url
@bp.route("/check_the_exam_url", methods=['GET', 'POST'])
def check_the_exam_url():
    id_test_paper = request.values.get("id_test_paper")
    test_paper = db.session.query(TestPaper).get(id_test_paper)
    test_paper_answer = test_paper.test_paper_answer

    students_result = []
    count = 0
    correct_count = 0
    for student in test_paper_answer:
        score = student.score
        id_test_paper_answer = student.id

        for i in student.question_answer:
            count += 1
            if i.true_or_false == 1:
                correct_count += 1

        students_result.append({
            "score": score,
            "student_name": student.student.name,
            "id_test_paper_answer": id_test_paper_answer
        })

    content = {
        "user": g.user,
        "students_result": students_result,
        "test_paper": test_paper,
    }

    return render_template("teacher/check_the_exam.html", **content)


# todo url /view_a_class_url
@bp.route("/view_a_class_url")
def view_a_class_url():
    id_classes = request.values.get("id_classes")
    classes = db.session.query(Classes).get(id_classes)
    student_classes_rel = db.session.query(StudentClassesRel).filter(StudentClassesRel.id_classes == id_classes).all()

    content = {
        "user": g.user,
        "student_classes_rel": student_classes_rel,
        "classes": classes,
    }
    return render_template("teacher/view_a_class.html", **content)


# todo url /questions_list_url
@bp.route("/questions_list_url")
def questions_list_url():
    content = {
        "user": g.user,
    }
    return render_template("teacher/questions_list.html", **content)


# todo url /sign_in_table_url
@bp.route("/sign_in_table_url")
def sign_in_table_url():
    content = {
        "user": g.user,
    }
    return render_template("teacher/sign_in_table.html", **content)


# todo url /sign_in_log_table_url
@bp.route("/sign_in_log_table_url")
def sign_in_log_table_url():
    content = {
        "user": g.user,
    }
    return render_template("teacher/sign_in_log_table.html", **content)


# todo url /sign_in_classes_url
@bp.route("/sign_in_classes_url")
def sign_in_classes_url():
    content = {
        "user": g.user,
        "classes": g.user.teacher.classes
    }
    return render_template("teacher/sign_in_classes.html", **content)


# todo url /sign_in_log_classes_url
@bp.route("/sign_in_log_classes_url")
def sign_in_log_classes_url():
    content = {
        "user": g.user,
        "classes": g.user.teacher.classes
    }
    return render_template("teacher/sign_in_log_classes.html", **content)


# todo url /see_a_student_sign_up_url
@bp.route("/see_a_student_sign_up_url")
def see_a_student_sign_up_url():
    id_student = request.values.get("id_student")
    sign_in = db.session.query(SignIn).filter(SignIn.id_student == id_student).all()
    content = {
        "user": g.user,
        "sign_in": sign_in
    }
    return render_template("teacher/see_a_student_sign_up.html", **content)


# todo url /see_a_student_score
@bp.route("/see_a_student_score")
def see_a_student_score():
    id_student = request.values.get("id_student")
    student = db.session.query(Student).filter(Student.id == id_student).first()
    test_paper_answer = db.session.query(TestPaperAnswer).filter(TestPaperAnswer.id_student == id_student).all()
    content = {
        "user": g.user,
        "test_paper_answer": test_paper_answer,
        "student": student
    }
    return render_template("teacher/see_a_student_score.html", **content)


# todo url /test_paper_result
@bp.route("/test_paper_result")
def test_paper_result():
    id_test_paper_answer = request.values.get("id_test_paper_answer")
    test_paper_answer = db.session.query(TestPaperAnswer).get(id_test_paper_answer)
    # print(test_paper_answer)

    single_choice_answer = []
    judgmental_answer = []
    short_answer_answer = []
    multiple_choice_question = []
    picture_question = []
    count = 0
    correct_count = 0
    for i in test_paper_answer.question_answer:
        count += 1
        if i.true_or_false == 1:
            correct_count += 1
        if i.base_question.question_type == 0:
            question = i.base_question.single_choice_question
            single_choice_answer.append({
                "count": count,
                "question": question.question,
                "a": question.a,
                "b": question.b,
                "c": question.c,
                "d": question.d,
                "answer": question.answer,
                "my_answer": i.my_answer,
                "true_or_false": '正确' if i.true_or_false == 1 else "错误"
            })
        elif i.base_question.question_type == 1:
            question = i.base_question.judgmental_question
            judgmental_answer.append({
                "count": count,
                "question": question.question,
                "answer": "对" if int(question.answer) == 1 else "错",
                "my_answer": "对" if int(i.my_answer) == 1 else "错",
                "true_or_false": '正确' if i.true_or_false == 1 else "错误"
            })
        elif i.base_question.question_type == 2:
            question = i.base_question.short_answer_question
            short_answer_answer.append({
                "count": count,
                "question": question.question,
                "answer": question.answer,
                "my_answer": i.my_answer,
                "true_or_false": '正确' if i.true_or_false == 1 else "错误"
            })
        elif i.base_question.question_type == 3:
            question = i.base_question.multiple_choice_question
            multiple_choice_question.append({
                "count": count,
                "question": question.question,
                "a": question.a,
                "b": question.b,
                "c": question.c,
                "d": question.d,
                "answer": question.answer,
                "my_answer": i.my_answer,
                "true_or_false": '正确' if i.true_or_false == 1 else "错误"
            })
        elif i.base_question.question_type == 4:
            question = i.base_question.picture_question
            picture_question.append({
                "count": count,
                "question": question.question,
                "answer": question.answer,
                "my_answer": i.my_answer,
                "true_or_false": '正确' if i.true_or_false == 1 else "错误"
            })

    content = {
        "user": g.user,
        "single_choice_answer": single_choice_answer,
        "judgmental_answer": judgmental_answer,
        "short_answer_answer": short_answer_answer,
        "multiple_choice_question": multiple_choice_question,
        "picture_question": picture_question,
        "score": test_paper_answer.score,
        "id_test_paper_answer": id_test_paper_answer
    }

    return render_template("teacher/test_paper_result.html", **content)
